<?php

use Illuminate\Database\Capsule\Manager as DB;
use app\modules\Admin\models\pickgoodscardModel;

class OrderController extends CommonController
{
    public function indexAction()
    {
        $params = $this->get();
        json(['code' => 1, 'msg' => '订单列表', 'data' => orderModel::getList($params)]);
    }

    public function couponsAction()
    {
        $params = $this->getPost();

        try {
            Validate::check($params, ['goods_list' => ['func'=>'isNotEmpty', 'msg'=>'商品']]);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }

        $data = orderModel::coupons($params['goods_list']);
        json(['code' => 1, 'msg' => '优惠券', 'data' => $data]);
    }

    public function preAction()
    {
        $params = $this->getPost();

        try {
            Validate::check($params, ['goods_list' => ['func'=>'isNotEmpty', 'msg'=>'商品']]);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }

        $data = orderModel::previewOrder($params);

        $delivery_card_id = $params['delivery_card_id'] ?? 0;
        if ($delivery_card_id) {
            $user = \app\service\auth::getUser();
            $member_card = DB::table('member_pickgoods_card')
                ->where('member_id', $user->id)
                ->find($delivery_card_id);
            if (!$member_card) {
                throw new Exception('用户提货卡不存在');
            }
            if ($member_card->money < $data['total_fee']) {
                throw new Exception('提货卡余额不足');
            }
            $data['delivery_card'] = $member_card;
        }

        json(['code' => 1, 'msg' => '计算成功', 'data' => $data]);
    }

    public function createorderAction()
    {
        $params = $this->getPost();

        try {
            Validate::check($params, ['goods_list' => ['func'=>'isNotEmpty', 'msg'=>'商品']]);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        try {
            $data = orderModel::createOrder($params);

            if (!$data) {
                json(['code' => 0, 'msg' => '订单提交失败']);
                exit;
            }
        }catch (Exception $e){
            json(['code' => 0, 'msg' => $e->getMessage()]);
            exit;
        }


        json(['code' => 1, 'msg' => '订单添加成功', 'data' => $data]);
    }

    public function detailAction()
    {
        $params = $this->get();
        $detail = orderModel::detail($params);
        $items = [];
        foreach ($detail['item'] as $item) {
            $goods_combo = $item['goods_combo'];
            if (is_string($goods_combo)) {
                $item['goods_combo'] = json_decode($goods_combo, true);
            }
            $items[] = $item;
        }
        $detail['item'] = $items;
        json(['code' => 1, 'msg' => '订单详情', 'data' => $detail]);
    }

    public function itemdetailAction()
    {
        $params = $this->get();
        json(['code' => 1, 'msg' => '订单商品', 'data' => orderModel::itemDetail($params)]);
    }

    public function cancelAction()
    {
        $params = ['id' => $this->get('id')];
        try {
            Validate::check($params, ['id' => ['role' => 'required']]);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        json(['code' => 1, 'msg' => '取消成功', 'data' => orderModel::operCancel($params)]);
    }

    public function deleteAction()
    {
        $params = ['id' => $this->get('id')];
        try {
            Validate::check($params, ['id' => ['role' => 'required']]);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        json(['code' => 1, 'msg' => '删除成功', 'data' => orderModel::operDelete($params)]);
    }


    // 确认收货
    public function confirmAction()
    {
        $params = $this->getPost();
        try {
            Validate::check($params, [
                'id' => ['role' => 'required'],
                'order_item_id' => ['role' => 'required'],
            ]);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        json(['code' => 1, 'msg' => '收货成功', 'data' => orderModel::operConfirm($params)]);
    }

    // 订单商品全部确认收货
    public function confirmAllAction()
    {
        $params = $this->getPost();
        try {
            Validate::check($params, ['id' => ['role' => 'required']]);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        json(['code' => 1, 'msg' => '收货成功', 'data' => orderModel::operConfirmAll($params)]);
    }

    // 评价订单
    public function commentAction()
    {
        $params = $this->getPost();
        try {
            Validate::check($params, [
                'id' => ['role' => 'required'],
                'order_item_id' => ['role' => 'required'],
                'level' => ['role' => 'required|between:1,5', 'func' => 'isNumber'],
                'content' => ['role' => 'required'],
            ]);
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
        json(['code' => 1, 'msg' => '评价成功', 'data' => orderModel::operComment($params)]);
    }
}
